package com.wowotou.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayedQueueConfig {
    private static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    private static final String DELAYED_QUEUE_NAME = "delayed.queue";
    private static final String DELAYED_ROUTING_KEY = "delayed.routingkey";

    @Bean("delayedExchange")
    public CustomExchange delayedExchange(){
        Map<String,Object> args = new HashMap<>();
        //交换机类型为direct，而消息是延迟消息
        args.put("x-delayed-type","direct");
        return new CustomExchange(DELAYED_EXCHANGE_NAME,"x-delayed-message",true,false,args);
    }

    @Bean("delayedQueue")
    public Queue delayedQueue(){
        return QueueBuilder
                       .durable(DELAYED_QUEUE_NAME)
                       .build();
    }

    @Bean
    public Binding delayedQueueBinding(@Qualifier("delayedQueue")Queue delayedQueue,@Qualifier("delayedExchange")CustomExchange delayedExchange){
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
    }
}
